import { KVAny, KVString } from "../type/kv";
import { Dictionary } from "../type/dictionary";

/**
 * mapper kv object with mapper you provided
 * @param object KVAny
 * @param mapper KVString
 */
export const kvMapper = (object: KVAny, mapper: KVString): KVAny => {
  const result: KVAny = { ...object };
  Object.entries(mapper).forEach(
    entry => (result[entry[1]] = result[entry[0]])
  );

  return result;
};

/**
 * mapper kv list object with mapper you provided
 * @param list KVAny[]
 * @param mapper KVString
 */
export const kvListMapper = (list: KVAny[], mapper: KVString): KVAny[] => {
  const result: KVAny[] = [];
  for (const kv of list) {
    result.push(kvMapper(kv, mapper));
  }

  return result;
};

/**
 * mapper kv list object to dictionary
 * @param list KVAny[]
 * @param key string, keyName of item in list
 * @param value string, valueName of item in list
 */
export const dictionaryMapper = (
  list: KVAny[],
  key: string,
  value: string
): Dictionary => kvListMapper(list, { [key]: value }) as Dictionary;
